package com.mrobuy.jd.api.apply;

import com.google.gson.Gson;
import com.jd.open.api.sdk.JdClient;
import com.jd.open.api.sdk.domain.supplier.ProductManagementService.response.get.AuditInfoDto;
import com.jd.open.api.sdk.request.supplier.VcItemProductApplyGetRequest;
import com.jd.open.api.sdk.response.supplier.VcItemProductApplyGetResponse;
import com.mrobuy.jd.api.JosClient;
import com.mrobuy.jdvc.entity.VCAuditInfo;
import com.mrobuy.jdvc.entity.VCAuditRecord;
import com.mrobuy.util.IdentifierGen;

import java.util.List;

/**
 *  根据商品编号获取商品信息
 *
 *  jingdong.vc.item.product.get
 *
 *  <url>https://open.jd.com/home/home#/doc/api?apiCateId=86&apiId=1672&apiName=jingdong.vc.item.product.get</url>
 *
 *
 *
 */
public class ProductApplyAuditGetClient extends JosClient<VCAuditInfo> {

    private static final int PAGE_SIZE = 10;

    private VcItemProductApplyGetRequest request=new VcItemProductApplyGetRequest();

    public ProductApplyAuditGetClient(JdClient client) {
        super(client);
    }

    @Override
    public VCAuditInfo call() throws Exception {

        VCAuditInfo auditInfo = null;

        LOG.info("[REQ]" + request.getApiMethod() + ": " + request.getAppJsonParams());

        VcItemProductApplyGetResponse response= client.execute(request);

        System.out.println(new Gson().toJson(response.getJosResultDto()));

        if ( response.getJosResultDto().getIsSuccess() ) {
            AuditInfoDto currentAuditInfo = response.getJosResultDto().getSingleObj().getCurrentAuditInfo();

            if ( currentAuditInfo!=null ) {
                auditInfo = new VCAuditInfo();
                auditInfo.setApplyId(request.getApplyId());
                auditInfo.setTaskid(currentAuditInfo.getTaskId());
                auditInfo.setApprovercode(currentAuditInfo.getApproverCode());
                auditInfo.setApprovername(currentAuditInfo.getApproverName());
                auditInfo.setOpinion(currentAuditInfo.getOpinion());
                auditInfo.setState(currentAuditInfo.getState());
                auditInfo.setApprovetime(currentAuditInfo.getApproveTime());
                auditInfo.setZt(1);

                List<AuditInfoDto> records = response.getJosResultDto().getSingleObj().getAuditRecords();
                if ( records!=null ) {
                    for (AuditInfoDto record : records) {
                        VCAuditRecord auditRecord = new VCAuditRecord();
                        auditRecord.setId(IdentifierGen.uuid());
                        auditRecord.setApplyId(request.getApplyId());
                        auditRecord.setTaskid(record.getTaskId());
                        auditRecord.setApprovercode(record.getApproverCode());
                        auditRecord.setApprovername(record.getApproverName());
                        auditRecord.setOpinion(record.getOpinion());
                        auditRecord.setState(record.getState());
                        auditRecord.setApprovetime(record.getApproveTime());
                        auditRecord.setZt(1);

                        auditInfo.getAuditRecords().add(auditRecord);
                    }
                }
            }

        }

        return auditInfo;
    }

    public ProductApplyAuditGetClient setApplyId(String applyId) {
        request.setApplyId(applyId);
        return this;
    }
}
